package cn.ultrarisk.scadmin.manager.web.controller.system;

import cn.ultrarisk.mybatis.extension.domain.Page;
import cn.ultrarisk.scadmin.manager.biz.common.dao.SchedulerJobDAO;
import cn.ultrarisk.scadmin.manager.biz.common.entity.SchedulerJob;
import cn.ultrarisk.scadmin.manager.biz.common.service.SchedulerJobService;
import cn.ultrarisk.scadmin.manager.web.controller.BaseController;
import cn.ultrarisk.scadmin.manager.web.dto.system.SchedulerJobDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * JobController
 *
 * @author sevendlong(lin23871@163.com)
 * @date 2014/4/17
 * @since V1.0
 */
@Controller
@RequestMapping(value = "/system/scheduler")
public class SchedulerController extends BaseController {

    @Autowired
    private SchedulerJobDAO schedulerJobDAO;
    @Autowired
    private SchedulerJobService schedulerJobService;

    @ResponseBody
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResponseEntity<Page<SchedulerJob>> list(String name, String remark, Integer status, int pageNo, int pageSize) {
        Page<SchedulerJob> schedulers = schedulerJobDAO.findPage(name, remark, status, pageNo, pageSize);
        return getOkResponseEntity(schedulers);
    }

    @ResponseBody
    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResponseEntity<String> create(@RequestBody SchedulerJobDTO dto) throws Exception {
        dto.setStatus(SchedulerJob.STATAUS_DISABLED);
        schedulerJobService.create(dto);
        return getOkResponseEntity();
    }

    @ResponseBody
    @RequestMapping(value = "/{id}", method = RequestMethod.POST)
    public ResponseEntity<String> update(@PathVariable("id") Integer id, @RequestBody SchedulerJobDTO dto) throws Exception {
        schedulerJobService.update(id, dto.getExpression(), dto.getRemark());
        return getOkResponseEntity();
    }

    @ResponseBody
    @RequestMapping(value = "/{id}/stop", method = RequestMethod.POST)
    public ResponseEntity<String> stop(@PathVariable("id") Integer id) throws Exception {
        schedulerJobService.disable(id);
        return getOkResponseEntity();
    }

    @ResponseBody
    @RequestMapping(value = "/{id}/start", method = RequestMethod.POST)
    public ResponseEntity<String> start(@PathVariable("id") Integer id) throws Exception {
        schedulerJobService.enable(id);
        return getOkResponseEntity();
    }

    @ResponseBody
    @RequestMapping(value = "/{id}/run", method = RequestMethod.POST)
    public ResponseEntity<String> run(@PathVariable("id") Integer id) throws Exception {
        schedulerJobService.execute(id);
        return getOkResponseEntity();
    }

    @ResponseBody
    @RequestMapping(value = "/sync", method = RequestMethod.POST)
    public ResponseEntity<String> sync() throws Exception {
        schedulerJobService.sync();
        return getOkResponseEntity();
    }

    @ResponseBody
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public ResponseEntity<String> delete(@PathVariable("id") Integer id) throws Exception {
        schedulerJobService.delete(id);
        return getOkResponseEntity();
    }
}
